package xiegd.study.Optional;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.Optional;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2021/11/4 9:24
 */
public class Demo1 {
    public static void main(String[] args) {

    }

    @Test
    public void test() {
        User user = null;//new User();
       // user.setName("woshiceshi");
       // user = Optional.ofNullable(user).orElse(createUser("1"));
        user = Optional.ofNullable(user).orElseGet(() -> createUser("3"));
    //    String jijfida = Optional.ofNullable(user.getName()).orElse("jijfida");
    //    System.out.println("输出:" + jijfida);
    //    Optional<String> name = Optional.ofNullable(user.getName());
    }
    public User createUser(String s){
        System.out.println("我被调用了》》》》》"+s);
        User user = new User();
        user.setName("zhangsan");
        return user;
    }

    @Test
    public void test2(){
        User myuser = new User();
        myuser.setAddress(new Address(""));
        String s = Optional.ofNullable(myuser)
                .map(u -> u.getAddress())
                .map(a -> a.getAddressName())
                .orElse("dfaf");
        System.out.println("我是测试：：：" + s);
    }
}


@Data
@NoArgsConstructor
@AllArgsConstructor
class User{
    private String name;
    private Integer age;
    private Address address;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Address{
    private String addressName;
}